/*
 * SystemClient_DBImpl.java
 *
 * Created on October 10, 2006, 8:27 PM
 */

package com.asaga.tools;

import java.beans.*;
import java.io.Serializable;
import java.util.Date;
import java.util.ArrayList;
import java.sql.*;
import org.apache.tools.ant.taskdefs.Java;

/**
 * @author checkout
 */
public class SystemClient_DBImpl extends Object implements SystemClient {
    private String fname;
    private int id;
    private String state;
    private String city;
    private String addr1;
    private String addr2;
    private String lname;
    private String zip;
    private String phone;
    private String cell;
    private String email;
    private String password;
    private String status;
    private String validation;
    private Date regDate;
    private Date lastVisit;
    private String welcomeMsg = "";

    ArrayList userList;
    
    User tempUser;
    private int permissionId;
    private int groupId;
    private int eventId;
    private int classId;
    
    // Added by Jeremy
    private JeremyDBConnectObj jdbcon;
    private String errmsg;
    private String dob;
    private String classification;
    //private ArrayList classification; // Classifications of user.
    
    
    public SystemClient_DBImpl() {

    }
    
    public boolean getProfile(String e, String p){
        // This method will query the database for all profile information for 
        // the given user.  It will place the information into its private vars.
        // Upon a successful retrieval of info, true will be returned otherwise
        // false is returned.
        
        // Check to see if valid inputs
        if(e==null){
            return false;
        }
        else if(p==null){
            return false;
        }
        
        // Query db using username.
        jdbcon = new JeremyDBConnectObj();
        Connection conn = jdbcon.getConnection();
        ResultSet rs = null;
        String query = "select * from person where email='"+e+"'";
        try {
            java.sql.Statement statement = conn.createStatement();
            rs = statement.executeQuery(query);
            
            // Check to see if email is already in the database.
            if(rs.next()) // Check if it is an empty ResultSet. If so, return false. 
            {
                if(rs.getString("email").equalsIgnoreCase(e)){
                    // Check to see if passwords match.
                    if(rs.getString("passwd").equals(p)){
                        this.fname = rs.getString("fname");
                        this.lname = rs.getString("lname");
                        this.addr1 = rs.getString("addr1");
                        this.addr2 = rs.getString("addr2");
                        this.city = rs.getString("city");
                        this.state = rs.getString("state");
                        this.zip = rs.getString("zip");
                        this.phone = rs.getString("phone");
                        this.cell = rs.getString("cell");
                        this.email = rs.getString("email");
                        this.password = rs.getString("passwd");
                        return true;
                    }
                    else{
                        return false;
                    }
                }  
            }
            else{
                return false;
            }           
        } catch (SQLException ex) {
            ex.printStackTrace();
            this.errmsg = ex.toString();
            return false;
        }        
        return false;
    }        
        
      
    
    
    public boolean updateProfile(String username){
        // This method will update the database with all private stored vars
        // for the given person.  Returns true upon a successful update
        // otherwise returns false.
        jdbcon = new JeremyDBConnectObj();
        Connection conn = jdbcon.getConnection();
        ResultSet rs = null;
        // Check to see if valid inputs
        if(username==null||!username.equals(this.email)){
            return false;
        }    
        else{        
            // Query db using username.
            String query = "select * from person where email='"+this.email+"'";
            try {
                java.sql.Statement statement = conn.createStatement();
                rs = statement.executeQuery(query);

                // Check to see if email is already in the database.
                if(rs.next())
                {
                    if(rs.getString("email").equalsIgnoreCase(this.email)){
                        this.errmsg = "User profile found!";
                    }  
                }
                else{
                    this.errmsg = "No valid user";
                    return false;
                }
            } catch (SQLException ex) {
                ex.printStackTrace();
                this.errmsg = ex.toString();
                return false;
            }       

            // Authentication successful, update data from priv vars to db.
            String updateQuery01 = "UPDATE person SET";
            String updateQuery02 = "fname='"+this.fname+"',lname='"+this.lname+"',addr1='"+this.addr1+"',addr2='"+this.addr2+"',city='"+this.city+"',state='"+this.state+"',zip='"+this.zip+"',phone='"+this.phone+"',cell='"+this.cell+"',dob='"+this.dob+"' ";
            //String updateQuery02 = "fname='Jeremy',lname='Sanderlin',addr1='54321',addr2='12345',city='athens',state='ga',zip='12345',phone='1234',cell='1234',dob='1234' ";
            String updateQuery03 = " WHERE email='"+this.email+"'";
            String updateQuery00 = updateQuery01+" "+updateQuery02+" "+updateQuery03;

            try {
                java.sql.Statement statement2 = conn.createStatement();
                int rowCount = statement2.executeUpdate(updateQuery00);
                this.errmsg = updateQuery00;

            } catch (SQLException ex) {
                ex.printStackTrace();
                this.errmsg = ex.toString();
                return false;
            }

            jdbcon.close();
            
            return true;
        }
    }
    
    public boolean createProfile(){
        jdbcon = new JeremyDBConnectObj();
        Connection conn = jdbcon.getConnection();
        ResultSet rs = null;
        if(this.email==null||this.fname==null||this.lname==null||this.password==null){
            this.errmsg="Invalid data was submitted.  Please try to register again.";
            return false;
        }
        String query = "select * from person where email='"+this.email+"'";
        try {
            java.sql.Statement statement = conn.createStatement();
            rs = statement.executeQuery(query);
            
            // Check to see if email is already in the database.
            if(rs.next())
            {
                if(rs.getString("email").equalsIgnoreCase(this.email)){
                    this.errmsg = "This email address is already being used.";
                    return false;
                }  
            }
            else{
                this.errmsg = "The email address is valid.";
            }
            
        } catch (SQLException ex) {
            ex.printStackTrace();
            this.errmsg = ex.toString();
            return false;
        }
     
        // This is where the update statements will be placed.
        String insertQuery01 = "INSERT INTO person ";
        String insertQuery02 = "(fname,lname,addr1,addr2,city,state,zip,email,passwd) ";
        String insertQuery03 = "VALUES ('"+this.fname+"','"+this.lname+"','"+this.addr1+"','"+this.addr2+"','"+this.city+"','"+this.state+"','"+this.zip+"','"+this.email+"','"+this.password+"')";
        String insertQuery00 = insertQuery01+" "+insertQuery02+" "+insertQuery03;
        
        try {
            java.sql.Statement statement2 = conn.createStatement();
            int rowCount = statement2.executeUpdate(insertQuery00);
            
        } catch (SQLException ex) {
            ex.printStackTrace();
            this.errmsg = ex.toString();
            return false;
        }
        
        jdbcon.close();
        return true; // for testing purposes only
    } 
    
    
    
    
    
    
    // Getters and Setters Listed Below...
    public String getErrorMessage(){
        return this.errmsg;
    }
    
    public void setId(int i){
        id = i;
    }
    
    public int getId(){
        return id;
    }

    public void setFname(String f) {
        fname = f;
    }

    public String getFname() {
        return fname;
    }

    public void setLname(String l) {
        lname = l;
    }

    public String getLname() {
        return lname;
        
    }

    public void setAddr1(String a1) {
        if(a1==null){
            addr1="null";
        }
        else{
            addr1 = a1;
        }
    }

    public String getAddr1() {
        return addr1;
    }

    public void setAddr2(String a2) {
        if(a2==null){
            addr2 = "null";
        }
        else{
            addr2 = a2;
        }
    }

    public String getAddr2() {
        return addr2;
    }

    public void setCity(String c) {
        if(c==null){
            city = "null";
        }
        else {
            city = c;
        }
    }

    public String getCity() {
        return city;
    }

    public void setState(String s) {
        if(s==null){
            state="null";
        }        
        else {
            state = s;
        }
    }

    public String getState() {
        return state;
    }

    public String getZip() {
        return this.zip;
    }

    public void setZip(String z) {
        if(z==null){
            zip = "null";
        }
        else {
            this.zip = z;
        }
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCell() {
        return cell;
    }

    public void setCell(String cell) {
        this.cell = cell;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String e) {
        this.email = e;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getValidation() {
        return validation;
    }

    public void setValidation(String validation) {
        this.validation = validation;
    }

    public Date getRegDate() {
        return regDate;
    }

    public void setRegDate(Date regDate) {
        this.regDate = regDate;
    }

    public Date getLastVisit() {
        return lastVisit;
    }

    public void setLastVisit(Date lastVisit) {
        this.lastVisit = lastVisit;
    }
    
    public String getDob(){
        return this.dob;
    }
    
    public void setDob(String d){
        this.dob = d;
    }
    
    public String getClassification(){
        return this.classification;
    }
    public void setClassification(String c){
        this.classification = c;
    }

    public void addUser() {
        int theID = userList.size();
        theID = theID +1;
        userList.add(new User(theID, fname, lname, email, password));
        id = theID;
        
    }

    public void updateUser(int userID) {
        tempUser = (User)userList.get(userID+1);
        tempUser.fname = fname;
        tempUser.lname = lname;
        tempUser.password = password;
        tempUser.email = email;
    }

    public void login(String email, String password){
        validation = "no";
        for(int i = 0; i < userList.size(); i++){
            tempUser = (User)userList.get(i);
            if(tempUser.email == email){
                if(tempUser.password == password){
                    validation = "yes";
                    break;
                }
                else
                    validation = "no";
            }
        }
        if(validation.equals("yes")){
            id = tempUser.userID;
            email = tempUser.email;
            fname = tempUser.fname;
            lname = tempUser.lname;
            password = tempUser.password;
        }
    }
    
    private class User {
        public int userID;
        public String fname;
        public String lname;
        public String email;
        public String password;
        public User(int id, String f, String l, String e, String p){
            userID = id;
            fname = f;
            lname = l;
            email = e;
            password = p;
        }
    }
    
}
